package com.janke.softballnexus.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.google.appengine.api.datastore.KeyFactory;
import com.janke.softballnexus.server.EMF;

@Entity
public class Division {

	public static void deleteAllDivions() {
		EntityManager em = entityManager();
		try {
			em.createQuery(
					"DELETE FROM " + Division.class.getSimpleName() + " d")
					.executeUpdate();
		} finally {
			em.close();
		}
	}

	public static final EntityManager entityManager() {
		return EMF.get().createEntityManager();
	}

	@SuppressWarnings("unchecked")
	public static List<Division> findAllDivions() {
		EntityManager em = entityManager();
		try {
			List<Division> list = em.createQuery(
					"SELECT d FROM " + Division.class.getSimpleName()
							+ " d ORDER BY d.year").getResultList();
			list.size();
			return list;
		} finally {
			em.close();
		}
	}

	public static Division findDivision(Long id) {
		if (id == null) {
			return null;
		}
		EntityManager em = entityManager();
		try {
			Division division = em.find(Division.class,
					KeyFactory.createKey(Division.class.getSimpleName(), id));
			return division;
		} finally {
			em.close();
		}
	}

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@NotNull
	@Size(min = 3, max = 15, message = "A division name must be no less than 3 characters long, and no more than 15 characters long")
	private String name;

	private Long leagueId;

	@NotNull
	@Min(value = 5, message = "Minimum age is 5 years")
	@Max(value = 16, message = "Minimum age is 16 years")
	private Integer minimumAge;

	@NotNull
	@Min(value = 5, message = "Minimum age is 5 years")
	@Max(value = 16, message = "Minimum age is 16 years")
	private Integer maximumAge;

	@NotNull
	@Min(value = 2005, message = "The earliest year that can be specified in 2005")
	private Integer year;

	@Version
	@Column(name = "version")
	@NotNull
	@DecimalMin("0")
	private Integer version = 0;

	public Division() {
	}

	public Long getId() {
		return id;
	}

	public Long getLeagueId() {
		return leagueId;
	}

	public Integer getMaximumAge() {
		return maximumAge;
	}

	public Integer getMinimumAge() {
		return minimumAge;
	}

	public String getName() {
		return name;
	}

	public Integer getVersion() {
		return version;
	}

	public Integer getYear() {
		return year;
	}

	public void persist() {
		EntityManager em = entityManager();
		try {
			em.persist(this);
		} finally {
			em.close();
		}
	}

	public void remove() {
		EntityManager em = entityManager();
		try {
			em.remove(this);
		} finally {
			em.close();
		}
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setLeagueId(Long leagueId) {
		this.leagueId = leagueId;
	}

	public void setMaximumAge(Integer maximumAge) {
		this.maximumAge = maximumAge;
	}

	public void setMinimumAge(Integer minimumAge) {
		this.minimumAge = minimumAge;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public void setYear(Integer year) {
		this.year = year;
	}

	@Override
	public String toString() {
		return "Division " + name + " (" + year + ")";
	}

}
